﻿package com.video{
	
	import flash.display.Sprite;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	import flash.events.NetStatusEvent;
	import flash.media.Video;
	import flash.events.Event;
	
	public class SimpleVideo extends Sprite{
		
		private var nc:NetConnection;
		private var video:Video;
		private var ns:NetStream;
		
		private var looping:Boolean = true;
		
		public function SimpleVideo(w:int, h:int):void{
			nc = new NetConnection();
			nc.connect(null);
			ns = new NetStream(nc);
			
			video = new Video();
			video.width = w;
			video.height = h;
			video.attachNetStream(ns);
			video.smoothing = true;
			addChild(video);
			
			ns.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus, false, 0, true);
			ns.client = this;
		}
		
		public function playVideo(filename:String):void {
			ns.play(filename);
		}

		private function onNetStatus(e:NetStatusEvent):void {
			var info:Object = e.info;
			
			switch(info.code) {
				case "NetStream.Play.Stop":
					if (looping) {
						ns.seek(0);
						ns.play(0);
					} else {
						dispatchEvent(new Event(Event.COMPLETE));
					}
					break;
				case "NetStream.Play.Start":
					dispatchEvent(new Event("ready"));
					break;
				default:
					//
			}
		}
		
		public function onMetaData(info:Object):void {
//			trace("Meta data received:" + info);
		}
		
	}
}